package com.dxc.intern.controller;


import com.dxc.intern.entity.FileStoreUtil;
import com.dxc.intern.entity.Page;
import com.dxc.intern.entity.QrCode;
import com.dxc.intern.entity.Result;
import com.dxc.intern.service.QrCodeService;
import com.dxc.untils.result.ResultUtil;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.itextpdf.text.Image;
import net.minidev.json.JSONUtil;
import org.apache.catalina.webresources.FileResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 二维码(求职者扫码操作) 前端控制器
 * </p>
 *
 * @author zhangchengming
 * @since 2022-02-21
 */
@CrossOrigin
@RestController
@RequestMapping("/qrCode")
public class QrCodeController {

    @Autowired
    private QrCodeService qrCodeService;
    @Autowired
    private FileStoreUtil fileStoreUtil;

    /**
     * 分页查询二维码
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Result<Page<QrCode>> page(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){
        Page<QrCode> page =qrCodeService.page(pageNo,pageSize);

        return Result.success("二维码分页", page);
    }

    /**
     * 分页模糊查询二维码
     *
     * @param pageNo
     * @param pageSize
     * @param qrCode
     * @return
     */
    @PostMapping("/selectLike")
    public Result<Page<QrCode>> selectLike(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                           @RequestBody QrCode qrCode){
        Page<QrCode> page =qrCodeService.selectLike(pageNo,pageSize,qrCode);

        return Result.success("二维码模糊分页", page);
    }



    /**
     * 添加二维码
     *
     * @param qrCode
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody QrCode qrCode){
        boolean b=qrCodeService.insert(qrCode);
        return b ? Result.success("添加成功", true) :
                Result.error("添加失败", false);
    }

    /**
     * 删除二维码 通过二维码id
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public Result<String> delete(@RequestParam(name = "id") Integer id){
        boolean b=qrCodeService.delete(id);
        return b ? Result.success("删除成功", qrCodeService.toString()) :
                Result.error("删除失败", "false");
    }
    /**
     * 更新二维码状态
     *
     * @param qrCode
     * @return
     */
    @PutMapping("/update")
    public Result<String> update(@RequestBody QrCode qrCode){
        boolean b=qrCodeService.update(qrCode);
        return b ? Result.success("更新成功",qrCode.toString()) :
                Result.error("更新失败", qrCode.toString());

    }
    /**
     * 导出二维码
     *
     * @param qrAddr
     * @return
     */
    @RequestMapping("/encode")
    public ResultUtil encode(@RequestParam(name = "qrAddr" )String qrAddr) throws WriterException, IOException {

        int width = 100;
        int height = 100;
        Map<EncodeHintType, Object> encodeHints = new HashMap<EncodeHintType, Object>();
        encodeHints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(qrAddr, BarcodeFormat.QR_CODE, width, height, encodeHints);

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix,"PNG",byteArrayOutputStream);

        MatrixToImageWriter.writeToPath(bitMatrix,"png",new File("D://erweima.png").toPath());
        InputStream inputStream= new ByteArrayInputStream(byteArrayOutputStream.toByteArray());


        String path = fileStoreUtil.uploadFile(inputStream,"png");
        System.out.println(path);
        return ResultUtil.success(fileStoreUtil.path+path);

//        return fileStoreUtil.uploadFile(inputStream,"png");

    }


}
